//var win = Titanium.UI.currentWindow;
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

// create tab group
var tabGroup = Titanium.UI.createTabGroup(
{
	barColor:'#336699'
});


var win = Titanium.UI.createWindow({  
    title:'DotNetNuke Photo Snap',
    backgroundColor:'#fff'
});

var tab1 = Titanium.UI.createTab({
    //icon:'images/tabs/KS_nav_views.png',
    //title:'',
    window:win
});


tabGroup.addTab(tab1);

tabGroup.setActiveTab(1);
// open tab group with a transition animation
tabGroup.open({
	transition:Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT
});

win.backgroundColor = 'black';
win.show();
var userPhoto;
var userPhotoName;

var dialog = Titanium.UI.createOptionDialog({
	options:['Create Media with Camera', 'Select Media from Library', 'Cancel'],
	cancel:2,
	title:'Camera'
});

var userPhotoView = Titanium.UI.createImageView({
    width:100,
    height:100,
    top:10
});

var userPhotoLabel = Titanium.UI.createLabel({
    text:'Enter Photo Name',
    height:40,
    width:200,
    color:'#fff',
    textAlign:'center',
    top:120
});

var photoNameTextField = Titanium.UI.createTextField({
	color:'#336699',
	height:40,
	top:160,
	width:200,
	borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

var submitButton = Titanium.UI.createButton({
    title:'Nuke That Photo!',
    height:40,
    width:200,
    top:210
});

dialog.show();

dialog.addEventListener('click',function(e)
{
	if (e.index == 0) {
        createNew();
    }
    else if (e.index == 1) {
        selectExisting();
    }
});

submitButton.addEventListener('click',function(e)
{
    photoNameTextField.blur();
    win.add(ind);
    ind.show();
    userPhotoName = photoNameTextField.value;
    uploadPhoto(userPhoto, userPhotoName);
});

var ind=Titanium.UI.createProgressBar({
    width:200,
    height:50,
    min:0,
    max:1,
    value:0,
    style:Titanium.UI.iPhone.ProgressBarStyle.BAR,
    top:260,
    message:'Uploading Image',
    font:{fontSize:12, fontWeight:'bold'},
    color:'#888'
});

Ti.Gesture.addEventListener('shake', deviceShake);
 
function deviceShake() {
    photoNameTextField.blur();
    win.add(ind);
    ind.show();
    userPhotoName = photoNameTextField.value;
    uploadPhoto(userPhoto, userPhotoName);
}

function createNew(){
    Titanium.Media.showCamera({
        success:function(event)
        {
            setupForm(event.media);
        },
        cancel:function()
        {
        },
        error:function(error)
        {
            ind.hide();
            alert(error.message);
        }
    });
}

function setupForm(media){
    userPhoto = media;
    userPhotoView.image = userPhoto;
    win.add(userPhotoView);
    win.add(userPhotoLabel);
    win.add(photoNameTextField);
    win.add(submitButton);
}

function selectExisting() {

    Titanium.Media.openPhotoGallery({
        success:function(event)
        {
            setupForm(event.media);
        },
        cancel:function()
        {
        },
        error:function(error)
        {
            ind.hide();
            alert(error.message);
        }
    });
}

function uploadPhoto(image, name) {

		var xhr = Titanium.Network.createHTTPClient();

		xhr.onerror = function(e)
		{
			Ti.UI.createAlertDialog({title:'Error', message:e.error}).show();
			Ti.API.info('IN ERROR ' + e.error);
		};
        
		xhr.setTimeout(20000);
        
		xhr.onload = function(e)
		{
            Ti.UI.createAlertDialog({title:'Success', message:'Your photo has been uploaded to your DotNetNuke website!'}).show();
			Ti.API.info('IN ONLOAD ' + this.status + ' readyState ' + this.readyState);
            ind.hide();
		};
        
		xhr.onsendstream = function(e)
		{
			ind.value = e.progress ;
			Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
		};
        
		// open the client
		xhr.open('POST','http://r2istl.staging.r2integrated.com/Gallery/iPhone.aspx');

		// send the data
		xhr.send({photo:image,name:name});
}